new Vue({
  el: '#app',
  data() {
    return {
      form: {
        serachKey: "",
        page: 1,			//页数
        limit: 10			//每页数据条数
      },
      formData: {},
      data: [],
      dataArr: [],
      tableData: [{}],		//表格数据
      article_info: [],
      store_info: [],
      supplier_article: [],
      supplier_info: [],
      total: 1,
      dialogForm: false,
      dialogVisible: false,
      dialogVisible1: false,
      title: '',
      fileList: [],
      multipleSelection: [],
      loading: false,
      form2: []
    }
  },
  mounted() {
    const start_date = sessionStorage.getItem("start-date");
    const end_date = sessionStorage.getItem("end-date");
    this.getList1(start_date, end_date)
  },
  computed: {
    formatTime() {
      return function (time) {
        if (time) {
          let d = new Date(parseInt(time));
          return `${d.getFullYear()}-${d.getMonth() + 1
            }-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`;
        } else {
          return ''
        }
      };
    },
    Article_Name() {
      return function (id) {
        if (this.article_info.length > 0) {
          let obj = this.article_info.filter(item => {
            return item.Article_SerialNumber == id
          })[0]
          return obj.Article_Name
        } else {
          return id
        }

      }
    },
    Article_SerialNumber() {
      return function (id) {
        if (this.article_info.length > 0) {
          let obj = this.article_info.filter(item => {
            return item.Article_Name == id
          })[0]
          console.log(obj);
          return obj.Article_SerialNumber
        } else {
          return id
        }

      }
    },
    Store_Name() {
      return function (id) {
        if (this.store_info.length > 0) {
          let obj = this.store_info.filter(item => {
            return item.Store_SerialNumber == id
          })[0]
          return obj.Store_Name
        } else {
          return id
        }
      }
    },
    Store_SerialNumber() {
      return function (id) {
        if (this.store_info.length > 0) {
          let obj = this.store_info.filter(item => {
            return item.Store_Name == id
          })[0]
          return obj.Store_SerialNumber
        } else {
          return id
        }
      }
    },
    Supplier_Name() {
      return function (id) {
        if (this.supplier_info.length > 0) {
          let obj = this.supplier_info.filter(item => {
            return item.Supplier_SerialNumber == id
          })[0]
          return obj.Supplier_Name
        } else {
          return id
        }
      }
    },
    Supplier_SerialNumber() {
      return function (id) {
        if (this.supplier_info.length > 0) {
          let obj = this.supplier_info.filter(item => {
            return item.Supplier_Name == id
          })[0]
          return obj.Supplier_SerialNumber
        } else {
          return id
        }
      }
    },
    Supplier_Article() {
      return function (id) {
        if (this.supplier_article.length > 0) {
          let arr = this.supplier_article.filter(item => {
            return item.Article_SerialNumber == id
          })
          let str = ""
          for (let i = 0; i < arr.length; i++) {
            if (str == "") {
              str = str + arr[i].Supplier_Name
            } else {
              str = str + "," + arr[i].Supplier_Name
            }
          }
          return str
        } else {
          return id
        }
      }
    },
  },
  methods: {
    onSerach() {				//查询方法
      console.log("搜搜")
    },
    addContract() {
      console.log("跳转")
    },
    //订单分配
    AddMember() {
      if (this.multipleSelection.length > 0) {
        this.dialogVisible = true
        this.data = []
        //获取每个订单的商品流水号
        this.multipleSelection.forEach(item => {
          //生成提交的json
          this.data.push({
            Order_SerialNumber: '',
            Sales_Order_Detail_SerialNumber: item.Sales_Order_Detail_SerialNumber,
            Store_SerialNumber: item.Store_SerialNumber,
            Article_SerialNumber: item.Article_SerialNumber,
            Quantity: item.Quantity,
            Supplier_SerialNumber: ''
          })
        });
      } else {
        this.$message({
          type: 'warning',
          message: '请先选择订单，再进行分配'
        })
      }
    },
    val(Arr) {
      for (let i = 0; i < Arr.length; i++) {
        if (Arr[i].Supplier_SerialNumber.length == 0) {
          return false
        }
      }
      return true
    },
    addEnterprise() {
      let _this = this
      this.loading = true
      $.ajax({
        type: 'POST',
        url: DomainName + '/OrderInfo_Add',
        data: {
          Data: JSON.stringify({
            order_info: _this.form2
          }),
          token: $.cookie("token")
        },
        success: function (data) {
          if (data.Status == "success") {
            _this.$message.success("提交成功")
            const start_date = sessionStorage.getItem("start-date");
            const end_date = sessionStorage.getItem("end-date");
            _this.dialogVisible1 = false
            _this.getList1(start_date, end_date)
          } else {
            _this.$message.error("提交失败，请稍后再试")
          }
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    submitForm() {
      let _this = this
      if (this.val(this.data)) {
        _this.loading = true
        $.ajax({
          url: DomainName + '/OrderInfo_Add',
          type: 'POST',
          data: {
            Data: JSON.stringify({
              order_info: _this.data
            })
          },
          success: function (res) {
            if (res.Status === 'success') {
              _this.$message.success("提交成功")
              const start_date = sessionStorage.getItem("start-date");
              const end_date = sessionStorage.getItem("end-date");
              _this.dialogVisible = false;
              _this.getList1(start_date, end_date)

            } else {
              _this.$message.error("提交失败，请稍后再试")
            }
          },
          error: function () {
            _this.$message.error("操作失败，请稍后再试")
          },
          complete: function (XMLHttpRequest) {
            _this.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          },
        })
      } else {
        this.$message.error('请先选择订单，再进行分配')
      }
    },
    getList1(start_date, end_date) {
      let _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + '/GoodsFileInquiry',
        data: {
          Data: 'article_info'
        },
        success: function (data) {
          _this.article_info = data
          console.log(data);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
      $.ajax({
        type: 'POST',
        url: DomainName + '/GoodsFileInquiry',
        data: {
          Data: 'store_info'
        },
        success: function (data) {
          _this.store_info = data
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
      $.ajax({
        type: 'POST',
        url: DomainName + '/GoodsFileInquiry',
        data: {
          Data: 'supplier_info'
        },
        success: function (data) {
          console.log(data);
          _this.supplier_info = data
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
      $.ajax({
        type: 'GET',
        data: { action: "jQueryAjax" },
        url: DomainName + '/Supplier_Article',
        data: {},
        success: function (res) {
          _this.supplier_article = res
          console.log(res);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
      $.ajax({
        type: 'POST',
        data: {
          start_date,
          end_date
        },
        url: DomainName + '/SalesOrderTime',
        success: function (data) {
          _this.tableData = data.filter(item => {
            return item.Row_Status == '1'
          })
          console.log(data);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    getList() {     //换页方法
      let _this = this
      $.ajax({
        type: 'POST',
        //	url: "../getContractListByConditionAction",
        url: "../getListByTableNameAction",
        data: {
          Data: "enterprise_info"
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          console.log(Result);
          _this.tableData = Result      //将获取的数据存到表格数据中
        },
        dataType: "json"
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.dataArr = []
      this.multipleSelection.forEach(item => {
        //下拉选项
        let chars = this.supplier_article.filter(item_ => {
          return item_.Article_SerialNumber == item.Article_SerialNumber
        })
        this.dataArr.push(chars)
      })
      console.log(this.dataArr);
    },
    back() {
      window.location.href = '../xiaoshou/xiaoshou.html'
    },
    onExcel() {
      /* generate workbook object from table */
      //  .table要导出的是哪一个表格
      var wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
      /* get binary string as output */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        //  name+'.xlsx'表示导出的excel表格名字
        saveAs(
          new Blob([wbout], {
            type: "application/octet-stream"
          }),
          "销售订单" + ".xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
    beforeRemove(file, fileList) {
      this.fileList.pop()
      this.form2 = []
    },
    uploadDetails(file) {
      this.fileList.push({
        name: file.name,
        url: file.type
      })
      let _this = this
      var reader = new FileReader();
      reader.readAsBinaryString(file);
      reader.onload = function (e) {
        var data = e.target.result;
        var wb = XLSX.read(data, {
          type: 'binary' //以二进制的方式读取
        });
        var sheet0 = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
        var str = XLSX.utils.sheet_to_json(sheet0);//利用接口实现转换。
        console.log(str);
        //将整体数据分解成每一条，一条上传一次

        //1.将接受到数组便利
        //2.将栏目存到_this.sales_unitprice里并去重
        str.forEach(item => {
          _this.form2.push({
            Order_SerialNumber: '',
            Sales_Order_Detail_SerialNumber: item['销售订单明细流水号'],
            Store_SerialNumber: _this.Store_SerialNumber(item['门店号']),
            Article_SerialNumber: _this.Article_SerialNumber(item['商品']),
            Quantity: item['数量'],
            Supplier_SerialNumber: _this.Supplier_SerialNumber(item['可选择供应商'])
          })
        })
      }
    },
  },
})